package com.xuyu.nacos.callback.listener.analysekey.onkey;


import com.xuyu.nacos.callback.listener.analysekey.AbstractGetKey;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.lang.reflect.Field;

/**
 * @author xuyu
 * 处理字段上带了@ConfigurationProperties的结构化配置
 */
@Component
public class WithConfigurationPropertiesAnnotation extends AbstractGetKey<Field> {

    @Override
    public String getKey(Field field) {
        Class<?> declaringClass = field.getDeclaringClass();
        ConfigurationProperties annotation = declaringClass.getAnnotation(ConfigurationProperties.class);
        return annotation.prefix() + "." + field.getName();
    }

    @Override
    public boolean choose(Field field) {
        Class<?> declaringClass = field.getDeclaringClass();
        return declaringClass.isAnnotationPresent(ConfigurationProperties.class) &&
                !field.isAnnotationPresent(Value.class);
    }

}
